/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class MailboxMaintenance
/*     */ {
/*     */   private final String accountId;
/*     */   private final int mailboxId;
/*     */   private Mailbox mailbox;
/*     */   private List<Thread> allowedThreads;
/*  31 */   private boolean nestedAllowed = false;
/*  32 */   private boolean inner = false;
/*     */   
/*     */   MailboxMaintenance(String acct, int id) {
/*  35 */     this(acct, id, null);
/*     */   }
/*     */   
/*     */   MailboxMaintenance(String acct, int id, Mailbox mbox) {
/*  39 */     this.accountId = acct.toLowerCase();
/*  40 */     this.mailboxId = id;
/*  41 */     this.mailbox = mbox;
/*  42 */     this.allowedThreads = new ArrayList();
/*  43 */     this.allowedThreads.add(Thread.currentThread());
/*     */   }
/*     */   
/*     */   String getAccountId() {
/*  47 */     return this.accountId;
/*     */   }
/*     */   
/*     */   int getMailboxId() {
/*  51 */     return this.mailboxId;
/*     */   }
/*     */   
/*     */   Mailbox getMailbox() {
/*  55 */     return this.mailbox;
/*     */   }
/*     */   
/*     */   void setMailbox(Mailbox mbox) {
/*  59 */     if ((mbox.getId() == this.mailboxId) && (mbox.getAccountId().equalsIgnoreCase(this.accountId))) {
/*  60 */       this.mailbox = mbox;
/*     */     }
/*     */   }
/*     */   
/*     */   synchronized void registerOuterAllowedThread(Thread t) throws MailServiceException {
/*  65 */     if (this.inner)
/*  66 */       throw MailServiceException.MAINTENANCE(this.mailboxId, "cannot add new maintenance thread when inner maintenance is already started");
/*  67 */     if (!this.nestedAllowed)
/*  68 */       throw MailServiceException.MAINTENANCE(this.mailboxId, "cannot add outer maintenance thread when nested is not enabled");
/*  69 */     if (this.allowedThreads.size() > 0) {
/*  70 */       throw MailServiceException.MAINTENANCE(this.mailboxId, "cannot add multiple outer maintenance threads");
/*     */     }
/*  72 */     registerAllowedThread(t);
/*     */   }
/*     */   
/*     */   public synchronized void registerAllowedThread(Thread t) {
/*  76 */     this.allowedThreads.add(t);
/*     */   }
/*     */   
/*     */   synchronized void removeAllowedThread(Thread t) {
/*  80 */     this.allowedThreads.remove(t);
/*     */   }
/*     */   
/*     */   synchronized void setNestedAllowed(boolean allowed) {
/*  84 */     this.nestedAllowed = allowed;
/*     */   }
/*     */   
/*     */   synchronized void startInnerMaintenance() throws MailServiceException {
/*  88 */     if (this.inner)
/*  89 */       throw MailServiceException.MAINTENANCE(this.mailboxId, "attempted to nest maintenance when already nested");
/*  90 */     if ((!this.nestedAllowed) || (!canAccess())) {
/*  91 */       throw MailServiceException.MAINTENANCE(this.mailboxId, "attempted to nest maintenance when not allowed");
/*     */     }
/*  93 */     this.inner = true;
/*     */   }
/*     */   
/*     */   synchronized boolean endInnerMaintenance() {
/*  97 */     boolean set = this.inner;
/*  98 */     assert ((this.nestedAllowed) || (!set));
/*  99 */     this.inner = false;
/* 100 */     return set;
/*     */   }
/*     */   
/*     */   synchronized boolean isNestedAllowed() {
/* 104 */     return this.nestedAllowed;
/*     */   }
/*     */   
/*     */   synchronized boolean canAccess()
/*     */   {
/* 109 */     return this.allowedThreads.contains(Thread.currentThread());
/*     */   }
/*     */   
/*     */   synchronized void markUnavailable() {
/* 113 */     this.mailbox = null;
/* 114 */     this.inner = false;
/* 115 */     this.nestedAllowed = false;
/* 116 */     this.allowedThreads.clear();
/*     */   }
/*     */   
/*     */   void cacheMailbox(Mailbox mbox) {
/* 120 */     if ((mbox.getId() == this.mailboxId) && (mbox.getAccountId().equalsIgnoreCase(this.accountId))) {
/* 121 */       this.mailbox = mbox;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/MailboxMaintenance.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */